package com.mobile.sync.client.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class AbstractBean {
	protected Map<String, String> headers = new HashMap<String, String>();
	protected byte[] dataChunk;

	public void put(final String headerKey, final String headerVal) {
		if (isEmpty(headerKey) || isEmpty(headerVal)) {
			throw new IllegalArgumentException(
					"header key or value cannot be null/empty");
		}
		headers.put(headerKey, headerVal);
	}

	public boolean containsHeader(final String headerKey) {
		if (isEmpty(headerKey)) {
			throw new IllegalArgumentException("header Empty");
		}
		return headers.containsKey(headerKey);
	}

	public boolean deleteHeader(String headerKey) {
		if (isEmpty(headerKey)) {
			return false;
		}
		return null != headers.remove(headerKey);
	}

	public String getStringHeaderValue(String headerKey) {
		if (isEmpty(headerKey)) {
			return "";
		}
		return headers.get(headerKey);
	}

	public byte[] getDataChunk() {
		return dataChunk;
	}

	public void setDataChunk(byte[] dataChunk) {
		this.dataChunk = dataChunk;
	}

	public int getIntHeaderValue(String headerKey) {
		try {
			String headerValue = headers.get(headerKey);
			return Integer.parseInt(headerValue);
		} catch (NumberFormatException ex) {
			throw ex;
		}
	}

	@Override
	public String toString() {
		StringBuilder blder = new StringBuilder();
		Set<String> headerKeys = headers.keySet();
		for (String headerKey : headerKeys) {
			blder.append("[" + headerKey + ": " + headers.get(headerKey) + "]");
		}

		return blder.toString();
	}

	private boolean isEmpty(String value) {
		try {
			if (value != null && value=="") {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

}
